﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Input;
using Microsoft.Win32;

namespace dkProfiler
{
    /// <summary>
    /// View Model
    /// </summary>
    public class ProfilerViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        Dispatcher dispatcher;

        const string ext = ".xml";
        const string filter = "dkProfiler profiles (.xml)|*.xml";

        OpenFileDialog openDialog;
        SaveFileDialog saveDialog;

        public ICommand NewProfileCommand { get; set; }
        public ICommand LoadProfileCommand { get; set; }
        public ICommand SaveProfileCommand { get; set; }

        public Profile ActiveProfile { get; set; }
        public string ActiveProfilePath { get; set; }

        public ProfilerViewModel()
        {
            dispatcher = Dispatcher.CurrentDispatcher;

            this.NewProfileCommand = new NewProfileCommand(this);
            this.LoadProfileCommand = new LoadProfileCommand(this);
            this.SaveProfileCommand = new SaveProfileCommand(this);

            openDialog = new OpenFileDialog();
            openDialog.DefaultExt = ext;
            openDialog.Filter = filter;

            saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = ext;
            saveDialog.Filter = filter;

            NewProfile();
        }

        /// <summary>
        /// Create a new profile with the default values
        /// </summary>
        public void NewProfile()
        {
            ActiveProfile = new Profile();
            ActiveProfilePath = "";

            ActiveProfile.Stats.Recalculate();
            OnPropertyChanged(null);
        }

        /// <summary>
        /// Load profile from a file
        /// </summary>
        public void LoadProfile()
        {
            Nullable<bool> result = openDialog.ShowDialog();
            if (result == true)
            {
                Profile profile = Profile.LoadFromFile(openDialog.FileName);
                if (profile != null)
                {
                    ActiveProfile = profile;
                    ActiveProfilePath = openDialog.FileName;

                    OnPropertyChanged(null);
                }
            }
        }

        /// <summary>
        /// Import external data to the current profile
        /// </summary>
        public void Import()
        {
            // NYI
        }

        /// <summary>
        /// Save profile to a file
        /// </summary>
        public void SaveProfile()
        {
            saveDialog.FileName = ActiveProfilePath;
            Nullable<bool> result = saveDialog.ShowDialog();
            if (result == true)
            {
                Profile.SaveToFile(ActiveProfile, saveDialog.FileName);
                ActiveProfilePath = saveDialog.FileName;

                OnPropertyChanged(null);
            }
        }
    }
}
